﻿////using System;
////using System.Collections.Generic;
////using FT.Architecture.NHibernate.SessionManagement;
////using FT.Architecture.Queries;
////using NHibernate;
////using NHibernate.Criterion;

////namespace FT.Architecture.NHibernate.Queries
////{
////    public class CriteriaExecutorService : IQueryExecutorService
////    {
////        private readonly ISessionProviderService sessionProvider;

////        public CriteriaExecutorService(ISessionProviderService sessionProvider)
////        {
////            this.sessionProvider = sessionProvider;
////        }

////        public IEnumerable<T> Execute<T>(Query<T> query) where T : class
////        {
////            var session = sessionProvider.GetSession();
////            var criteria = session.CreateCriteria(typeof(T));
////            criteria = TranslateIntoNHibernateQuery(query, criteria);

////            if (query.FirstResult.HasValue)
////            {
////                criteria.SetFirstResult(query.FirstResult.Value);
////            }

////            if (query.MaxResults.HasValue)
////            {
////                criteria.SetMaxResults(query.MaxResults.Value);
////            }

////            return criteria.List<T>();
////        }

////        private static ICriteria TranslateIntoNHibernateQuery<T>(Query<T> query, ICriteria criteria)
////        {
////            BuildQueryFrom(query, criteria);

////            if (query.OrderByProperty.IsSet)
////            {
////                criteria.AddOrder(new Order(query.OrderByProperty.PropertyName, !query.OrderByProperty.Descending));
////            }

////            return criteria;
////        }

////        private static void BuildQueryFrom<T>(Query<T> query, ICriteria criteria)
////        {
////            IList<ICriterion> criterionCollection = new List<ICriterion>();

////            if (query.Criteria == null)
////            {
////                return;
////            }

////            foreach (var c in query.Criteria)
////            {
////                ICriterion criterion;

////                switch (c.CriteriaOperator)
////                {
////                    case CriteriaOperator.Equal:
////                        criterion = Restrictions.Eq(c.PropertyName, c.Value);
////                        break;
////                    case CriteriaOperator.NotEqual:
////                        criterion = Restrictions.Not(Restrictions.Eq(c.PropertyName, c.Value));
////                        break;
////                    case CriteriaOperator.LessThanOrEqual:
////                        criterion = Restrictions.Le(c.PropertyName, c.Value);
////                        break;
////                    case CriteriaOperator.GreaterThanOrEqual:
////                        criterion = Restrictions.Ge(c.PropertyName, c.Value);
////                        break;
////                    case CriteriaOperator.GreaterThan:
////                        criterion = Restrictions.Gt(c.PropertyName, c.Value);
////                        break;
////                    case CriteriaOperator.LessThan:
////                        criterion = Restrictions.Lt(c.PropertyName, c.Value);
////                        break;
////                    case CriteriaOperator.Like:
////                        criterion = Restrictions.Like(c.PropertyName, c.Value + "%");
////                        break;
////                    case CriteriaOperator.IsNotNull:
////                        criterion = Restrictions.IsNotNull(c.PropertyName);
////                        break;
////                    case CriteriaOperator.IsNull:
////                        criterion = Restrictions.IsNull(c.PropertyName);
////                        break;
////                    default:
////                        throw new ApplicationException("No operator defined");
////                }

////                criterionCollection.Add(criterion);
////            }

////            foreach (var fetch in query.Fetches)
////            {
////                criteria.SetFetchMode(fetch.PropertyName, FetchMode.Eager);
////            }

////            Junction subQuery;
////            if (query.QueryOperator == QueryOperator.And)
////            {
////                subQuery = Restrictions.Conjunction();
////            }
////            else
////            {
////                subQuery = Restrictions.Disjunction();
////            }
////            foreach (var criterion in criterionCollection)
////            {
////                subQuery.Add(criterion);
////            }

////            criteria.Add(subQuery);

////            foreach (var sub in query.SubQueries)
////            {
////                BuildQueryFrom(sub, criteria);
////            }
////        }
////    }
////}
